<?php

class newmetrics_model extends CI_Model {
	//Obtain record_id for a particular server
	//RETURN: array $record_id
	   function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
	//prototype function
	function get_counter_name($servername)
	{
		$query=$this->db->query("select counters from perfmon.servers where name='$servername'");
		$row=$query->row();
		$counter_id_array=explode(',',$row->counters);
		return $counter_id_array;
	}
	//function return array of counter_id, counter_name
	function get_counter_id_name($servername)
	{
		//determine counters for each server
		$query=$this->db->query("SELECT DISTINCT counter_id from perfmon.$servername order by counter_id ASC");	
		$i=0;	
		foreach ($query->result_array() as $row)
		{
   			$counter_array[$i]=$row['counter_id'];
			$i++;
		}
		return $counter_array;
	}
	//function to extract data for each counter_id and for each server
	function get_counter_value($servername)
	{
		//$counter_id=$this->get_counter_id_name($servername);	
		$counter_id=$this->get_counter_name($servername);	
		for($j=0;$j<sizeof($counter_id);$j++)
		//foreach($counter_id as $id)
		{
			$id=$counter_id[$j];
			$query=$this->db->query("select name from perfmon.counters where id=$id");
			$row=$query->row();
			$counter_name=$row->name;
			//------------------------------------------------	
			$query=$this->db->query("select counter_name, timestamp,value from perfmon.$servername where counter_id=$id order by timestamp desc");
			$i=0;
			foreach($query->result_array() as $row)
			{
				$reading[$i]=array(($row['timestamp'])*1000, $row['value']);
				$i++;
			}
			$rawdata[$counter_name]=json_encode($reading);
		}
			//$cnames=array_keys($rawdata);			
			//$content['data']=json_encode($rawdata[$cnames[0]]);
			//$content['graph_name']=$cnames[0];
			//$this->load->view('view',$content);	;		;
			return $rawdata;
	}
		

}
?>
